import Cookies from "js-cookie";
import { getInfo } from "@/api/login";

const state = {
  sidebar: {
    opened: Cookies.get("sidebarStatus") ? !!+Cookies.get("sidebarStatus") : true,
    withoutAnimation: false,
    hide: false
  },
  device: "desktop",
  size: Cookies.get("size") || "medium",
  deptId: "",
  deptName: ""
};

const mutations = {
  TOGGLE_SIDEBAR: (state) => {
    if (state.sidebar.hide) {
      return false;
    }
    state.sidebar.opened = !state.sidebar.opened;
    state.sidebar.withoutAnimation = false;
    if (state.sidebar.opened) {
      Cookies.set("sidebarStatus", 1);
    } else {
      Cookies.set("sidebarStatus", 0);
    }
  },
  CLOSE_SIDEBAR: (state, withoutAnimation) => {
    Cookies.set("sidebarStatus", 0);
    state.sidebar.opened = false;
    state.sidebar.withoutAnimation = withoutAnimation;
  },
  TOGGLE_DEVICE: (state, device) => {
    state.device = device;
  },
  SET_SIZE: (state, size) => {
    state.size = size;
    Cookies.set("size", size);
  },
  SET_DEPT_ID: (state, deptId) => {
    state.deptId = deptId;
  },
  SET_SIDEBAR_HIDE: (state, status) => {
    state.sidebar.hide = status;
  },
  // 拼接后的机构名称
  SET_DEPT_NAME: (state, deptName) => {
    state.deptName = deptName;
  }
};

const actions = {
  // 获取用户信息
  getInfo({ commit, state }) {
    return new Promise((resolve, reject) => {
      getInfo()
        .then((res) => {
          const user = res.data.user;
          const avatar = user.avatar == "" || user.avatar == null ? require("@/assets/images/profiles.jpg") : user.avatar;
          // if (user.dept.labFlag != 1) {
          //   commit("SET_DEPT_ID", null);
          // } else {
          //   commit("SET_DEPT_ID", user.deptId);
          // }
          resolve(res);
        })
        .catch((error) => {
          reject(error);
        });
    });
  },

  toggleSideBar({ commit }) {
    commit("TOGGLE_SIDEBAR");
  },
  closeSideBar({ commit }, { withoutAnimation }) {
    commit("CLOSE_SIDEBAR", withoutAnimation);
  },
  toggleDevice({ commit }, device) {
    commit("TOGGLE_DEVICE", device);
  },
  setSize({ commit }, size) {
    commit("SET_SIZE", size);
  },
  toggleSideBarHide({ commit }, status) {
    commit("SET_SIDEBAR_HIDE", status);
  }
  // setDeptName({ commit }, deptName) {
  //   commit('SET_DEPT_NAME', deptName)
  // },
};

export default {
  namespaced: true,
  state,
  mutations,
  actions
};
